use base64::prelude::*;
use crate::tool_result::Error;

pub fn string_to_bytes(s: &str) -> Vec<u8> {
    s.as_bytes().to_vec()
}

pub fn bytes_to_string_utf8(b: &[u8]) -> String {
    String::from_utf8(b.to_vec()).unwrap()
}

pub struct Base64{}

impl Base64 {
    pub fn encode(b: &[u8])->String{
        BASE64_STANDARD.encode(b)
    }
    pub fn decode(s:String)->Result<Vec<u8>,Error>{
        match BASE64_STANDARD.decode(s){
            Ok(re)=>Ok(re),
            Err(e)=>Err(Error::err(e.to_string()))
        }
    }
}

pub struct StringConvert{}

impl StringConvert {
    pub fn to_int_i32<T:Into<String>>(str: T)->Result<i32,Error>{
        let re:Result<i32,_>= str.into().parse();
        match re{
            Ok(re)=>Ok(re),
            Err(e)=>Err(Error::err(e.to_string()))
        }
    }
    pub fn to_int_i64<T:Into<String>>(str: T)->Result<i64,Error>{
        let re:Result<i64,_>= str.into().parse();
        match re{
            Ok(re)=>Ok(re),
            Err(e)=>Err(Error::err(e.to_string()))
        }
    }
    pub fn to_int_u64<T:Into<String>>(str: T)->Result<u64,Error>{
        let re:Result<u64,_>= str.into().parse();
        match re{
            Ok(re)=>Ok(re),
            Err(e)=>Err(Error::err(e.to_string()))
        }
    }
}
